package com.qf.gh.web.servlet;

import com.qf.gh.domain.Address;
import com.qf.gh.domain.Cart;
import com.qf.gh.domain.Order;
import com.qf.gh.domain.User;
import com.qf.gh.service.AddressService;
import com.qf.gh.service.CartService;
import com.qf.gh.service.OrderService;
import com.qf.gh.service.impl.AddressServiceImpl;
import com.qf.gh.service.impl.CartServiceImpl;
import com.qf.gh.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet{
    CartService cartService=new CartServiceImpl();
    AddressService addressService=new AddressServiceImpl();
    OrderService orderService=new OrderServiceImpl();
    public String getOrderView(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        List<Cart> carts= cartService.findByUid(user.getId());
        List<Address> addList=addressService.findByUid(user.getId());
        request.setAttribute("addList",addList);
        request.setAttribute("carts",carts);
        return "forward:/order.jsp";
    }
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        String aid = request.getParameter("aid");
        Order order = orderService.addOrder(user.getId(), Integer.parseInt(aid));
        request.setAttribute("order", order);
        return "forward:/orderSuccess.jsp";
    }


}
